// pages/order/order.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['全部订单', '待付款', '进行中', '已完成'],
        currentTab: 0,
        orders: [
          {
            orderId: '1001',
            createTime: '2023-09-18 10:30',
            status: 1,
            statusText: '待付款',
            flavor: '草莓味',
            spec: '500g/瓶',
            imageUrl: 'https://xxx.xjdio.com/static/img/1.jpg',//'https://picsum.photos/200/200?random=1',
            count: 2,
            totalPrice: 56.00
          },
          {
            orderId: '1002',
            createTime: '2023-09-17 15:20',
            status: 2,
            statusText: '制作中',
            flavor: '红枣味',
            spec: '500g/瓶',
            imageUrl: 'https://xxx.xjdio.com/static/img/1.jpg',//'https://picsum.photos/200/200?random=2',
            count: 1,
            totalPrice: 28.00
          },
          {
            orderId: '1003',
            createTime: '2023-09-16 09:15',
            status: 3,
            statusText: '配送中',
            flavor: '蓝莓味',
            spec: '500g/瓶',
            imageUrl: 'https://xxx.xjdio.com/static/img/1.jpg',//'https://picsum.photos/200/200?random=3',
            count: 3,
            totalPrice: 84.00
          },
          {
            orderId: '1004',
            createTime: '2023-09-15 14:00',
            status: 4,
            statusText: '已完成',
            flavor: '芒果味',
            spec: '500g/瓶',
            imageUrl: 'https://xxx.xjdio.com/static/img/1.jpg',//'https://picsum.photos/200/200?random=4',
            count: 2,
            totalPrice: 56.00
          },
          {
            orderId: '1005',
            createTime: '2023-09-14 11:45',
            status: 5,
            statusText: '已完成',
            flavor: '蜜桃味',
            spec: '500g/瓶',
            imageUrl: 'https://xxx.xjdio.com/static/img/1.jpg',//'https://picsum.photos/200/200?random=5',
            count: 1,
            totalPrice: 28.00
          }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            filteredOrders: this.filterOrders(0)
          });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    switchTab(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
          currentTab: index,
          filteredOrders: this.filterOrders(index)
        });
      },
    
      filterOrders(tabIndex) {
        if (tabIndex === 0) return this.data.orders;
        const statusMap = [null, 1, 2, 4]; // 对应tab的订单状态
        return this.data.orders.filter(order => {
          if (tabIndex === 3) return order.status === 4 || order.status === 5;
          return order.status === statusMap[tabIndex];
        });
      },
    
      cancelOrder(e) {
        const orderId = e.currentTarget.dataset.id;
        wx.showModal({
          title: '提示',
          content: '确定要取消该订单吗？',
          success: (res) => {
            if (res.confirm) {
              this.updateOrderStatus(orderId, 0);
              wx.showToast({
                title: '订单已取消',
                icon: 'success'
              });
            }
          }
        });
      },
    
      payOrder(e) {
        const orderId = e.currentTarget.dataset.id;
        // 模拟支付流程
        wx.showLoading({
          title: '支付中...',
        });
        setTimeout(() => {
          wx.hideLoading();
          this.updateOrderStatus(orderId, 2);
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });
        }, 1500);
      },
    
      applyAfterSale(e) {
        const orderId = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: `/pages/afterSale/apply?orderId=${orderId}`,
        });
      },
    
      updateOrderStatus(orderId, newStatus) {
        const orders = this.data.orders.map(order => {
          if (order.orderId === orderId) {
            let statusText = '';
            switch(newStatus) {
              case 0: statusText = '已取消'; break;
              case 2: statusText = '制作中'; break;
              case 3: statusText = '配送中'; break;
              case 4: statusText = '已完成'; break;
            }
            return {...order, status: newStatus, statusText};
          }
          return order;
        });
        
        this.setData({
          orders,
          filteredOrders: this.filterOrders(this.data.currentTab)
        });
      },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})